package com.apes.fico.engine.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.plugin.soe.metadata.BusinessType;
import com.apes.framework.util.ApesConst;
import com.apes.fico.masterdata.gl.model.AccountingModel;
import com.apes.fico.masterdata.gl.model.AccountingSystem;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: apestech
 * @description: 凭证模板
 * @author: Chengxm
 * @create: 2019-01-24 15:41
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = VoucherTemplate.CONST_TABLE_NAME)
public class VoucherTemplate extends Model {
    public final static String CONST_TABLE_NAME = "fin_voucher_template";
    @Id
    @Column(name = "id", length = 8)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "凭证模板")
    @Comment("编码")
    private String id;

    @Comment("模板组")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "group_id", referencedColumnName = "id", nullable = false)
    private VoucherTemplateGroup voucherTemplateGroup;

    @Comment("科目体系")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "acc_system_id", referencedColumnName = "id", nullable = false)
    private AccountingSystem accountingSystem;

    @Comment("公司字段")
    @Column(name = "company_field", length = 64)
    private String companyField;


    @Comment("公司代码")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    private PartyRole company;

    @Name
    @Comment("描述")
    @Column(name = "description", length = 64)
    private String description;

    @Comment("业务类型")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "business_type", referencedColumnName = "id", nullable = false)
    private BusinessType businessType;

    @Comment("记账日期")
    @Column(name = "date_field", nullable = false, length = 64)
    private String dateField;

    @Comment("订阅条件")
    @Column(name = "subscription", nullable = false, length = 400)
    private String subscription;

    @States(name = "结转周期", values = {
            @State(id = AccountingModel.RT, name = "按单"),
            @State(id = AccountingModel.D, name = "日"),
            @State(id = AccountingModel.M, name = "月")})
    @Column(name = "cycle", length = 10, nullable = false)
    private String cycle;

    @Comment("策略") /*按某个维度生成单张凭证(dept,jzrq)*/
    @Column(name = "dimension", length = 200)
    private String dimension;


    @OneToMany(mappedBy = "voucherTemplate") //, cascade = CascadeType.ALL, orphanRemoval = true
    @OrderBy("sequence asc")
    private List<VoucherTemplateEntry> voucherTemplateEntries = new ArrayList();


    @Column(name = "valid", columnDefinition = ApesConst.FIELD_BOOLEAN_TRUE)
    @Comment("有效标记")
    private boolean valid = true; //有效

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        VoucherTemplate that = (VoucherTemplate) o;

        return id.equals(that.getId());
    }
}
